package com.A21MobileSales;

import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;

import com.A21.Object.A21OrderHeader;
import com.A21.Object.Global;
import com.A21.Object.MSTCustomer;
import com.A21.Object.SMGSTType;
import com.A21MobileSales.DataBase.DatabaseHelper;

@SuppressWarnings("deprecation")
public class OrderTab extends TabActivity {
	View tabView;
	Button btn_Home;
	ImageButton btn_back;
	private String CustCode, CName, Id, TrnType, GST, GSTInc,
			DiscountType = "A", DiscountPer, Currency;
	public String Screen = "";
	DatabaseHelper objDBHelper;
	TextView tv_Title;
	public int current_tab, previous_tab;
	TabHost tabHost;
	TextView tabHeader, tabDetail, tabFrequent, tabProduct;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.order_tab);

		if (getIntent().hasExtra("Code")) {
			CustCode = getIntent().getStringExtra("Code");
		}
		if (getIntent().hasExtra("CName")) {
			CName = getIntent().getStringExtra("CName");
		}
		if (getIntent().hasExtra("Id")) {
			Id = getIntent().getStringExtra("Id");
		}
		if (getIntent().hasExtra("TrnType")) {
			TrnType = getIntent().getStringExtra("TrnType");
		}

		if (getIntent().hasExtra("Screen")) {
			Screen = getIntent().getStringExtra("Screen");
		}
		objDBHelper = new DatabaseHelper(this);
		tv_Title = (TextView) findViewById(R.id.tv_Title);
		String tblname = "";
		if (TrnType.equals("CW")) {
			tblname = "A21OPOrderHeader";
			tv_Title.setText("SALES ORDER");
		} else if (TrnType.equals("CA")) {
			tblname = "A21OPCashInvoiceHeader";
			tv_Title.setText("CASH INVOICE");
		} else if (TrnType.equals("CR")) {
			tblname = "A21OPCreditInvoiceHeader";
			tv_Title.setText("CREDIT INVOICE");
		}
		if (!Id.equals("0")) {
			A21OrderHeader Header = objDBHelper
					.GetOrderHeaderByIdOrCreatedTime(tblname, Id, true);
			GST = String.valueOf(Header.GST);
			GSTInc = String.valueOf(Header.GSTInc);
			DiscountType = Header.DiscountType;
			DiscountPer = String.valueOf(Header.Discount);
			Currency = Header.Currency;
		} else {
			MSTCustomer customer = null;
			try {
				customer = objDBHelper.GetCustomerByCode(CustCode);
			} catch (Exception e) {
				Toast.makeText(OrderTab.this, e.getMessage(),
						Toast.LENGTH_SHORT).show();
			}
			if (customer != null) {
				SMGSTType gstType = objDBHelper.GetGSTByCode(customer.GSTType);
				GST = String.valueOf(gstType.GST);
				GSTInc = customer.GSTInc ? "1" : "0";
				Currency = customer.Currency;
			}
			DiscountPer = "0";
		}
		tabHost = getTabHost();

		btn_Home = (Button) findViewById(R.id.btn_Home);
		btn_Home.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent _intent = new Intent();
				_intent.setClass(OrderTab.this, HomeScreen.class);
				startActivity(_intent);
			}
		});

		btn_back = (ImageButton) findViewById(R.id.btn_back);
		btn_back.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				GoBack();
			}
		});
		current_tab = 0;
		previous_tab = 0;
		TabSpec spec;
		Intent intent;

		//
		tabHeader = new TextView(this);
		tabHeader.setHeight(60);
		tabHeader.setText("Header");
		tabHeader.setGravity(Gravity.CENTER);
		tabHeader.setTextColor(Color.WHITE);
		tabHeader.setBackgroundResource(R.drawable.bg_item_tab_title);
		tabHeader.setClickable(true);
		tabHeader
				.setTextSize(getResources().getDimension(R.dimen.Tab_TextSize));
		intent = new Intent().setClass(OrderTab.this, OrderHeader.class);
		Bundle b = new Bundle();
		b.putString("Code", CustCode);
		b.putString("CName", CName);
		b.putString("Id", Id);
		b.putString("TrnType", TrnType);
		b.putString("GST", GST);
		b.putString("GSTInc", GSTInc);
		b.putString("DiscountPer", DiscountPer);
		b.putString("DiscountType", DiscountType);
		intent.putExtras(b);
		spec = tabHost.newTabSpec("Header").setIndicator(tabHeader)
				.setContent(intent);
		tabHost.addTab(spec);

		//
		tabDetail = new TextView(this);
		tabDetail.setHeight(60);
		tabDetail.setText("Detail");
		tabDetail.setGravity(Gravity.CENTER);
		tabDetail.setTextColor(Color.WHITE);
		tabDetail.setBackgroundResource(R.drawable.bg_item_tab_title);
		tabDetail.setClickable(true);
		tabDetail
				.setTextSize(getResources().getDimension(R.dimen.Tab_TextSize));
		intent = new Intent().setClass(OrderTab.this, OrderDetail.class);
		intent.putExtras(b);
		spec = tabHost.newTabSpec("Detail").setIndicator(tabDetail)
				.setContent(intent);
		tabHost.addTab(spec);

		//
		tabFrequent = new TextView(this);
		tabFrequent.setHeight(60);
		tabFrequent.setText("Frequent");
		tabFrequent.setGravity(Gravity.CENTER);
		tabFrequent.setTextColor(Color.WHITE);
		tabFrequent.setBackgroundResource(R.drawable.bg_item_tab_title);
		tabFrequent.setClickable(true);
		tabFrequent.setTextSize(getResources().getDimension(
				R.dimen.Tab_TextSize));
		intent = new Intent().setClass(OrderTab.this, FrequentProduct.class);
		intent.putExtras(b);
		spec = tabHost.newTabSpec("Frequent").setIndicator(tabFrequent)
				.setContent(intent);
		tabHost.addTab(spec);

		//
		tabProduct = new TextView(this);
		tabProduct.setHeight(60);
		tabProduct.setText("Product");
		tabProduct.setGravity(Gravity.CENTER);
		tabProduct.setTextColor(Color.WHITE);
		tabProduct.setBackgroundResource(R.drawable.bg_item_tab_title);
		tabProduct.setClickable(true);
		tabProduct.setTextSize(getResources()
				.getDimension(R.dimen.Tab_TextSize));
		intent = new Intent().setClass(OrderTab.this, ProductSelection.class);
		intent.putExtras(b);
		spec = tabHost.newTabSpec("Product").setIndicator(tabProduct)
				.setContent(intent);
		tabHost.addTab(spec);

		try {
			Global.setMultiTierProduct(objDBHelper
					.GetAllMultiTierProductDisplay(CustCode, Currency));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		/*
		 * if(Global.getMultiTierProduct().size()>0){ tabH = new TextView(this);
		 * tabH.setText("Promotion"); tabH.setGravity(Gravity.CENTER);
		 * tabH.setTextColor(Color.WHITE);
		 * tabH.setBackgroundResource(R.drawable.tab_text_selector);
		 * tabH.setClickable(true);
		 * tabH.setTextSize(getResources().getDimension(R.dimen.Tab_TextSize));
		 * intent = new Intent().setClass(OrderTab.this,
		 * MTProductSelection.class); intent.putExtras(b); spec =
		 * tabHost.newTabSpec("Promotion").setIndicator(tabH)
		 * .setContent(intent); tabHost.addTab(spec); }
		 */

		tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			public void onTabChanged(String tag) {

				previous_tab = current_tab;
				current_tab = tabHost.getCurrentTab();

				eventSetCurrentTab(current_tab);

				switch (tabHost.getCurrentTab()) {
				case 0:
					OrderDetail activitydet = (OrderDetail) getLocalActivityManager()
							.getActivity("Detail");
					break;
				case 1:
					OrderDetail activity = (OrderDetail) getLocalActivityManager()
							.getActivity("Detail");
					if (Global.getSelectedTrnDetail() != null) {
						activity.UpdateDetailFromTab(Global
								.getSelectedTrnDetail());
						Global.setSelectedTrnDetail(null);
					}
					break;
				case 2:

					break;
				case 3:
					break;
				default:
					break;
				}
			}
		});
		tabHost.setup();
		// tabHost.setCurrentTab(1);
		tabHost.setCurrentTab(0);
		eventSetCurrentTab(0);

	}

	void eventSetCurrentTab(int positionCurrentTab) {
		switch (positionCurrentTab) {
		case 0:
			tabHeader
					.setBackgroundResource(R.drawable.ic_bg_tab_title_selected);
			tabHeader.setTextColor(getResources().getColor(
					R.color.colorTabSelected));

			tabDetail.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabDetail.setTextColor(Color.WHITE);

			tabFrequent.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabFrequent.setTextColor(Color.WHITE);

			tabProduct.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabProduct.setTextColor(Color.WHITE);
			break;
		case 1:
			tabDetail
					.setBackgroundResource(R.drawable.ic_bg_tab_title_selected);
			tabDetail.setTextColor(getResources().getColor(
					R.color.colorTabSelected));

			tabHeader.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabHeader.setTextColor(Color.WHITE);

			tabFrequent.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabFrequent.setTextColor(Color.WHITE);

			tabProduct.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabProduct.setTextColor(Color.WHITE);
			break;
		case 2:
			tabFrequent
					.setBackgroundResource(R.drawable.ic_bg_tab_title_selected);
			tabFrequent.setTextColor(getResources().getColor(
					R.color.colorTabSelected));

			tabDetail.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabDetail.setTextColor(Color.WHITE);

			tabHeader.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabHeader.setTextColor(Color.WHITE);

			tabProduct.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabProduct.setTextColor(Color.WHITE);
			break;
		case 3:
			tabProduct
					.setBackgroundResource(R.drawable.ic_bg_tab_title_selected);
			tabProduct.setTextColor(getResources().getColor(
					R.color.colorTabSelected));

			tabDetail.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabDetail.setTextColor(Color.WHITE);

			tabFrequent.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabFrequent.setTextColor(Color.WHITE);

			tabHeader.setBackgroundResource(R.drawable.ic_bg_tab_title);
			tabHeader.setTextColor(Color.WHITE);
			break;

		default:
			break;
		}
	}

	public void UpdateDetail() {
		OrderDetail activity = (OrderDetail) getLocalActivityManager()
				.getActivity("Detail");
		if (activity != null) {
			if (Global.getSelectedTrnDetail() != null) {
				activity.UpdateDetailFromTab(Global.getSelectedTrnDetail());
				Global.setSelectedTrnDetail(null);
			}
		}
	}

	public A21OrderHeader GetHeader() {
		OrderHeader acty = (OrderHeader) getLocalActivityManager().getActivity(
				"Header");
		return acty.Header;
	}

	private void GoBack() {
		Intent _intent = new Intent();
		Bundle b = new Bundle();
		if (Screen.equals("")) {
			b.putString("Code", CustCode);
			b.putString("CName", CName);
			b.putString("TrnType", TrnType);
		} else {
			b.putString("TrnType", "");
		}
		_intent.putExtras(b);
		_intent.setClass(OrderTab.this, OrderSummary.class);
		startActivity(_intent);
	}

}